<?php
/**
 * Created by PhpStorm.
 * User: zhang
 * Date: 2018/4/28
 * Time: 20:00
 */
namespace app\account\controller;

use app\account\model\User_id_util;
use app\account\model\GetSMS;
use app\common\model\Base;



class Register extends Base{

    private $user_id_util;
    private $alidayu;
    private $sms;


    function __construct(){

        $_SERVER["HTTP_X_TOKEN"] = "none";
        parent::__construct();
        $this->user_id_util = new User_id_util();
        $this->sms = new GetSMS();
    }

    /**
     *
     * @api {get} /account/register/page 注册页面
     * @apiDescription 注册页面
     * @apiGroup account
     */
    public function page(){
        return view("index");
    } 

    public function success1(){
        return view("success");
    }

    public function fail1(){
        return view("fail");
    }
    /**
     * 
     * @api {post} /account/register  注册接口
     * @apiDescription 注册接口
     * @apiGroup account
     * @apiVersion 0.1.0
     * @apiParam {string} tel 电话号码
     * @apiParam {string} authCode 验证码
     * @apiParam {string} passd 密码
     * @apiParam {string} refereeUid 推荐人uid
     *
     * @apiParamExample {json} Request-Example:
     *     {
     *       "tel": 17754928306，
     *       "passd": 123456789
     *       "authCode":"xxx"
     *       "refereeUid":"oVlguqTtPOZ0"
     *     }
     * @apiError {String} code 0 表示失败
     * @apiError {String} msg  "推荐人为空"，"注册失败，请稍后再试"
     *
     * @apiSuccess {String} code 1 表示成功.
     * @apiSuccess {String} msg 注册成功.
     * @apiSuccessExample {json} Success-Response:
     *     {
     *       "code": "1",
     *       "msg": "xxx"
     *     }
     */
    public function index()
    {
        $tel = $_GET["tel"];
        $authCode = $_GET["authCode"];
        $passd = $_GET["passd"];
        $refereeUid = $_GET["refereeUid"];

        if(empty($refereeUid)){
            echo echoJson(0, "推荐人为空");
            exit();
        }
        //if (isMD5Verify() === true) {//验证成功
        if (!$this->check->isRegisterNull($tel, $authCode, $passd)) {
            exit();
        }

        if(!$this->check->isTelExists($tel, $authCode, $passd,"REGISTER_ACCOUNT")){
            exit();
        }

        $this->getSelectUid($tel, $passd,$refereeUid);
       // } else {
       //     echo echoJson(0, "数据验证失败!");
       // }
    }


    function getSelectUid($tel, $passd, $refereeUid) {
        $sql = "SELECT id FROM user order by id desc limit 0,1";
        $row = $this->mysql->selectSQLRow($sql);
        if ($row) {//判断有没有上一条记录
            isset($row["id"]) ? $uid = ($row["id"] + 1) : $uid = '0';
            $uid = 'syid_' . ($uid);
        } else {
            $uid = 'syid_' . "0";
        }
        //include("../model/User_id_util.php");
        $this->registerSQL($this->user_id_util->createRandomID($uid), $tel, $passd,$refereeUid);
    }

    /**
     * 注册语句
     * @param str $tel 电话
     * @param str $passd 密码
     */
    function registerSQL($uid, $tel, $passd, $refereeUid) {
        $sql = "INSERT INTO user (uid,tel,passd) VALUES ('$uid','$tel','$passd')";
        $sql1 = "INSERT INTO agency_user_subordinate(uid,subordinate_id,subordinate_tel) VALUES ('$refereeUid','$uid','123')";

        if ($this->mysql->insertSQL($sql)&&$this->mysql->insertSQL($sql1)) {
            echo echoJson(1, "注册成功!");
        } else {
            echo echoJson(0, "注册失败，请稍后再试!");
        }
    }

    /**
     *
     * @api {post} /account/register/getAutoCode  注册获取验证码
     * @apiDescription 注册获取验证码
     * @apiGroup account
     * @apiVersion 0.1.0
     * @apiParam {string} tel 电话号码
     *
     * @apiError {String} code 0 表示失败
     * @apiError {String} msg  "xxxxx"
     *
     * @apiSuccess {String} code 1 表示成功.
     * @apiSuccess {String} msg 注册成功.
     */
    function getAutoCode(){
        $tel=$_GET["tel"];
        $this->sms->judgeSMSType($tel,"REGISTER_ACCOUNT");
    }
}